<template>
  <div>
    <div>
      <div @click="dialog = true">
        <el-tooltip class="user-avatar" effect="dark" content="更换头像" placement="bottom">
          <el-avatar :src="user.imgUrl" :size="100"></el-avatar>
        </el-tooltip>
      </div>
      <el-dialog title="更换头像" :visible.sync="dialog" width="30%" :close-on-click-modal="false">
        <el-upload style="text-align: center" action="" class="avatar-uploader" accept="image/jpg,image/jpeg,image/png"
          :show-file-list="false" :on-change="handleChange">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog = false">取 消</el-button>
          <el-button type="primary" @click="uploadAvatar">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <el-row>
      <div style="text-align: center; margin-bottom: 10px; margin-top: 10px">
        <span class="nickname">{{ user.nickname }}</span>
        <el-tag size="medium" :type="accountTag[user.disableStatus]">{{ account[user.disableStatus] }}</el-tag>
      </div>
    </el-row>

    <el-row style="text-align: center;">
      <el-popconfirm title="确定退出吗？" @confirm="logout" style="margin-right: 10px;">
        <el-button size="medium" type="primary" slot="reference">退 出</el-button>
      </el-popconfirm>
      <DropdownMenu></DropdownMenu>
    </el-row>

    <el-row style="margin-left: 10px;">
      <h1>个人信息</h1>
      <div style="font-size:14px;">
        <el-row class="user-info">手机号：{{ user.phone }}</el-row>
        <el-row class="user-info">QQ 邮箱：{{ user.qqEmail }}</el-row>
        <el-row class="user-info">加入时间：{{ user.gmtCreate }}</el-row>
      </div>
    </el-row>

    <el-row style="margin-left: 10px;">
      <h1>通知公告</h1>
    </el-row>
    <NoticeItems></NoticeItems>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { logout, updateUser } from "@/api/auth/user";
import { upLoadFile } from "@/api/file/file";
import DropdownMenu from "@/views/home/aside/components/dropdown/index.vue";
import NoticeItems from "@/views/home/aside/components/NoticeItems.vue";

export default {
  components: { NoticeItems, DropdownMenu },
  provide() {
    return {
      getUserInfo: this.getUserInfo,
    }
  },
  data() {
    return {
      user: {},
      account: ['正常', '封禁'],
      accountTag: ['success', 'danger'],
      dialog: false,
      imageUrl: '',
      file: new FormData()
    }
  },
  methods: {
    handleChange(file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.file.set("file", file.raw)
    },
    uploadAvatar() {
      upLoadFile(this.file).then(res => {
        if (res.data.code === 200) {
          this.user.imgUrl = res.data.imgUrl
          Cookies.set("user", JSON.stringify(this.user))
          this.getUserInfo()
          this.dialog = false
          updateUser(this.user).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.message)
            } else {
              // this.$message.error(res.data.message)
            }
          })
        } else {
          // this.$message.error(res.data.message)
        }
      })
    },
    logout() {
      logout().then(res => {
        if (res.data.code === 200) {
          Cookies.remove('user')
          Cookies.remove('user_token')
          this.$router.push(`/`)
        } else {
          // this.$message.error(res.data.message)
        }
      })
    },
    getUserInfo() {
      this.user = JSON.parse(Cookies.get("user"))
    }
  },
  created() {
    this.getUserInfo()
  }
}
</script>

<style lang="scss">
.user-avatar {
  margin: 50px 100px 0 100px;
  text-align: center;
}

.nickname {
  margin-right: 10px;
  font-weight: bold;
  font-size: 19px;
}

.user-info {
  margin-bottom: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
